
package com.emser.unc.server.servlets;

import java.io.IOException;
import java.security.Principal;

import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.emser.unc.serializable.UserAccountDTO;
import com.emser.unc.server.LoginHelper;
import com.google.appengine.api.users.UserService;
import com.google.appengine.api.users.UserServiceFactory;

@SuppressWarnings("serial")
public class LoginGoogleCallbackServlet extends HttpServlet {
  
  public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {
	  UserService userService = UserServiceFactory.getUserService();
	  Principal googleUser = request.getUserPrincipal();
    if (googleUser != null) {
      if(userService.isUserAdmin()){
    	  response.sendRedirect("https://www.google.com/a/cpanel/acloudmobile.com/CPanelHome#Organization");
    	  return;  
      }
      UserAccountDTO u = new UserAccountDTO();
	  u.setName(googleUser.getName());
      LoginHelper.loginStarts(request.getSession(), u);  
    }
    response.sendRedirect(LoginHelper.getApplitionURL(request));
  }
}
